import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../utils/search_module.dart';
import '../../published_daily_module/my_special_text_span_builder.dart';
import 'comment_model.dart';
import 'file_model.dart';
import '../../../services/address.dart';
import '../../../services/dio_manager.dart';
import 'my_daily_detail_model.dart';

class MyDailyDetailController extends GetxController {


  final MySpecialTextSpanBuilder mySpecialTextSpanBuilder =
  MySpecialTextSpanBuilder(showAtBackground: false);

  TextEditingController editingController = TextEditingController();


  MydailyDetailModel? model;
  FileModel? fileModel;

  var res_id;

  /// 获取数据
  requestData({var id}) async {
    var params = {
      'id': id,
      'report_type': '',
    };

    var json = await DioManager()
        .kkRequest(Address.reportInfo, method: 'get', params: params);
    MydailyDetailModel _model = MydailyDetailModel.fromJson(json);
    model = _model;

    editingController.text = _model.data!.content!;

    update();
  }

  /// 获取附件
  requestDataWithAttachment(var id) async {
    var json = await DioManager()
        .kkRequest('${Address.attachment}/crm.report/${id}', method: 'get');

    FileModel model = FileModel.fromJson(json);
    fileModel = model;
    update();
  }

  ///获取评论
  ///
  CommentModel? commentModel;

  requestDataWithComment(var id) async {
    var json = await DioManager()
        .kkRequest('${Address.commentList}/${id}', method: 'get');
    print('评论json========${json}');

    CommentModel model = CommentModel.fromJson(json);

    commentModel = model;

    update();
  }

  /// 评论
  requestDataWithMessageAdd(var res_id) async {
    var params = {
      'model': 'crm.report',
      'res_id': '${res_id}',
      'content': '${commentTextEditing.text}',
    };
    var json =
        await DioManager().kkRequest(Address.messageAdd, bodyParams: params);
    BotToast.showText(text: '${json['message']}');
    requestDataWithComment(Get.arguments);
  }

  TextEditingController commentTextEditing = TextEditingController();

  /// 发送评论
  tapClickSendComment() {
    if (commentTextEditing.text.isEmpty) {
      BotToast.showText(text: '评论不能为空');
      return;
    }
    requestDataWithMessageAdd(model?.data?.id);
    // requestDataWithComment();
    requestData(id: Get.arguments);
    commentTextEditing.text = '';
    SearchModule.closeKeyboard(Get.context!);
  }

  /// 点赞请求
  requestDataWithLike() async {
    var params = {
      'model': 'crm.report',
      'res_id': Get.arguments,
      'state': '1',
    };
    var json = await DioManager()
        .kkRequest(Address.resPraiseCreate, bodyParams: params);
    BotToast.showText(text: '${json['message']}');
    requestData(id: '${Get.arguments}');
  }

  /// 删除附件请求
  requestDataWithUnlink(var checksum) async {
    print('ssssss=====${res_id}');
    var params = {
      'id':checksum
    };
    var json = await DioManager().kkRequest('${Address.attachmentUnlink}/crm.report/${res_id}',bodyParams:params);
    BotToast.showText(text: '${json['message']}');
    requestDataWithAttachment(res_id);
  }

  /// 删除附近按钮
  void tapClicKUnlink(int index){
    requestDataWithUnlink(fileModel?.data?[index].checksum);
  }

  /// 点赞
  tapClickLike() {
    requestDataWithLike();
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    print('Get.arguments===${Get.arguments}');

    res_id = Get.arguments;

    //
    requestData(id: '${Get.arguments}');
    requestDataWithComment(Get.arguments);
    requestDataWithAttachment(Get.arguments);
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    // print('Get.arguments===${Get.arguments}');
    //
    // requestData(id: '${Get.arguments}');
    // requestDataWithComment(Get.arguments);
  }
}
